home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / croutes.zip / RANDOM.ASM < prev    next >
Assembly Source File  |  1984-07-29  |  2KB  |  68 lines

  1. PAGE   54,130
  2. ; RANDOM   - Random number generator for C programs
  3. ;
  4. ; This routine returns 16-bit unsigned pseudo-random numbers
  5. ; of uniform distribution.  The numbers are computed according
  6. ; to the Linear Congruential method as described in Knuth's
  7. ; "Seminumerical Algorithms",  using  the formula (ax) mod (w+1)
  8. ; as described on pp 11-12.
  9. ;
  10. ; No parameters are passed to this routine; the return is unsigned.
  11. ;
  12. ; The seed for each call is preserved form the previous call.
  13. ; The routine is "self seeding"; if the seed is found to contain
  14. ; zeros a DOS Get Time (x'2C') call is used to obtain the time, and
  15. ; the seconds and 100ths of seconds are used as the seed for
  16. ; subsiquent calls.
  17. ;
  18. ; However, for applications which require duplicatable series of terms
  19. ; (such as when testing), the seed, XRAND, is defined as an EXTERN
  20. ; and may be initialized by the calling program (via assignment)
  21. ; prior to calling RANDOM.
  22. ;
  23. ; The initial values for the multiplier and constant are borrowed
  24. ; from "A Guide to PL/M Programming for Microcomputer Applications",
  25. ; by Daniel D. McCracken (Addison-Wesley, 1978).
  26. ;
  27. PGROUP    GROUP  PROG
  28. PARAMS    STRUC
  29. OLD_BP    DW     ?           ; Caller's BP Save
  30. RETN    DW     ?           ; Return address from call
  31. PARAMS    ENDS
  32. ;
  33. DGROUP    GROUP  DATA
  34. DATA    SEGMENT WORD PUBLIC 'DATA'
  35.     ASSUME DS:DGROUP
  36.     PUBLIC XRAND
  37. XRAND    DW     0           ; "X" in formula - next/last term
  38. MULTIP    DW     2053           ; multiplier
  39. MODULUS DW     65535           ; constant
  40. DATA    ENDS
  41. PROG    SEGMENT BYTE PUBLIC 'PROG'
  42.     ASSUME CS:PGROUP
  43.     PUBLIC RANDOM
  44. ;
  45. RANDOM    PROC   NEAR
  46.     PUSH   BP           ; save caller's frame pointer
  47.     MOV    BP,SP           ; set up our frame pointer
  48.     MOV    AX,XRAND        ; move last random number to
  49.     CMP    AX,0           ; is it uninitialized?
  50.     JNE    GETNXT           ; ..no, skip this stuff
  51.     MOV    AH,2CH
  52.     INT    21H           ; .. get time from DOS
  53.     MOV    AX,DX           ; initialize w/ seconds, 100ths
  54. GETNXT: NEG    AX           ; negate the register
  55.     MUL    MULTIP           ; 32-bit multipl goes in DX,AX
  56.     MOV    XRAND,AX        ; save low order word for seed
  57.     SUB    DX,XRAND        ; subtract low word from high
  58.     JS     EXIT           ; if negative, skip next steps
  59.     INC    AX
  60.     INC    AX           ; increment term by 2
  61.     ADD    AX,MODULUS       ; add the increment
  62. EXIT:    POP    BP           ; restore the caller's frame pointer
  63.     RET               ; return to caller
  64. RANDOM    ENDP
  65. ;
  66. PROG    ENDS
  67.     END
  68.